package com.xxkj.drugtraceability.ui.hascollected

import android.graphics.Typeface
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.lifecycle.Observer
import androidx.viewpager.widget.ViewPager
import com.google.android.material.tabs.TabLayout
import com.xxkj.drugtraceability.R
import com.xxkj.drugtraceability.base.BaseVMActivity
import com.xxkj.drugtraceability.databinding.ActivityHascollectedBinding
import com.xxkj.drugtraceability.ext.clickN
import org.koin.androidx.viewmodel.ext.android.viewModel

/**
 * author: zhoujingjin
 * created on: 2024/9/18 15:38
 * description:已采集页面
 */
class HasCollectedActivity:BaseVMActivity() {
    private val binding by binding<ActivityHascollectedBinding>(R.layout.activity_hascollected)
    private val viewModel by viewModel<HasCollectedViewModel>()
    override fun initView() {
        binding.apply {
            toolBar.title = ""
            setSupportActionBar(toolBar)
            toolBar.setNavigationOnClickListener {
                onBackPressed()
            }
            viewModel.tabList.forEachIndexed { index, _ ->
                when(index){
                    0->viewModel.mFragmentList.add(OneFragment.newInstance())
                    1->viewModel.mFragmentList.add(TwoFragment.newInstance())
                    else->viewModel.mFragmentList.add(ThreeFragment.newInstance())
                }
            }
            llDate.clickN {
                when (val fragment = viewModel.mFragmentList[viewModel.currentPosition]) {
                    is OneFragment -> {
                        fragment.showDatePicker()
                    }
                    is TwoFragment -> {
                        fragment.showDatePicker()
                    }
                    is ThreeFragment -> {
                        fragment.showDatePicker()
                    }
                }
            }
            tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
                override fun onTabSelected(tab: TabLayout.Tab?) {
                    tab?.let {
                        val tabTextView = (tab.customView as? TextView)
                        tabTextView?.setTypeface(null, Typeface.BOLD)
                    }
                }

                override fun onTabUnselected(tab: TabLayout.Tab?) {
                    tab?.let {
                        val tabTextView = (tab.customView as? TextView)
                        tabTextView?.setTypeface(null, Typeface.NORMAL)
                    }
                }

                override fun onTabReselected(tab: TabLayout.Tab?) {
                }
            })

            viewPager.adapter = mAdapter
            viewPager.addOnPageChangeListener(object :ViewPager.OnPageChangeListener{
                override fun onPageScrollStateChanged(state: Int) {

                }

                override fun onPageScrolled(
                    position: Int,
                    positionOffset: Float,
                    positionOffsetPixels: Int
                ) {
                }

                override fun onPageSelected(position: Int) {
                    viewModel.currentPosition = position
                    //设置当前页面的日期时间
                    when (val fragment = viewModel.mFragmentList[viewModel.currentPosition]) {
                        is OneFragment -> {
                            fragment.setNowDate()
                        }
                        is TwoFragment -> {
                            fragment.setNowDate()
                        }
                        is ThreeFragment -> {
                            fragment.setNowDate()
                        }
                    }
                }
            })
            tabLayout.tabMode = TabLayout.MODE_SCROLLABLE
            tabLayout.tabGravity = TabLayout.GRAVITY_FILL
            tabLayout.setupWithViewPager(viewPager)
            viewPager.currentItem = 0
            for (i in 0 until tabLayout.tabCount) {
                val tab = tabLayout.getTabAt(i)
                val tabTextView = TextView(this@HasCollectedActivity)
                tabTextView.textSize = 15f
                tabTextView.setTextColor(resources.getColor(R.color.color_333333))
                if (i==0){
                    tabTextView.setTypeface(null, Typeface.BOLD)
                }
                tabTextView.text = tab?.text
                tab?.customView = tabTextView
            }
        }
    }

    override fun initData() {
    }

    override fun startObserve() {
        viewModel.dateTime.observe(this, Observer {
            binding.tvDate.text = it
        })
    }
    private val mAdapter: FragmentPagerAdapter =
        object : FragmentPagerAdapter(supportFragmentManager) {
            override fun getCount(): Int {
                // TODO Auto-generated method stub
                return viewModel.mFragmentList.size
            }

            override fun getItem(arg0: Int): Fragment {
                return viewModel.mFragmentList[arg0]
            }

            override fun getPageTitle(position: Int): CharSequence? {
                return viewModel.tabList[position]
            }
        }

}